<template>
  <div class="page-container main">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item>
          <el-input type="input" placeholder="openId" v-model="queryParams.openid" />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="queryParams.createBy"
            filterable
            remote
            reserve-keyword
            placeholder="操作人"
            remote-show-suffix
            :remote-method="queryUserList"
            :loading="loading"
            style="width: 240px"
          >
            <el-option v-for="item in users" :key="item.userId" :label="item.nickName" :value="item.userId" />
          </el-select>
          <!-- 
          <el-input type="input" placeholder="" v-model="queryParams.createBy" /> -->
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="times"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD hh:mm:ss"
              value-format="YYYY-MM-DD hh:mm:ss"
            />
          </el-form-item>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="tabel-container myTable">
      <vxe-table ref="tableRef" :data="tableData" v-loading="loading">
        <vxe-table-column :resizable="true" type="seq" title="序号" width="80" sortable></vxe-table-column>
        <vxe-table-column :resizable="true" field="openid" title="投放openID" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="num" title="投放张数" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="" title="电话" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="couponConsumerVos" title="劵ID" sortable>
          <template #default="{ row }">
            <span v-if="row.couponConsumerVos!=null">
              <p v-for="(item,index) in row.couponConsumerVos" :key="index">
                {{ item.couponId }}
              </p>
            </span>
            <span v-else>-</span>
          </template>
        </vxe-table-column>
        <vxe-table-column :resizable="true" field="couponConsumerVos" title="有效期起止" sortable width="300">
          <template #default="{ row }">
            <span v-if="row.couponConsumerVos!=null">
              <p v-for="(item,index) in row.couponConsumerVos" :key="index">{{ item.validTime }}-{{ item.expireTime }}</p>
            </span>
            <span v-else>-</span>
          </template>
        </vxe-table-column>
        <vxe-table-column :resizable="true" field="createByName" title="操作人" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="createTime" title="操作时间" sortable> </vxe-table-column>
      </vxe-table>
    </div>
    <div class="myPager">
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script lang="ts">
import { targetedList } from '@/api/operation/activity/index';
import { listUser } from '@/api/system/user/index';

import common from '@/utils/common.ts';

export default {
  props: {
    data: {}
  },
  computed: {
    times: {
      get() {
        return [this.queryParams.timeFrom, this.queryParams.timeTo];
      },
      set(value) {
        this.queryParams.timeFrom = value[0];
        this.queryParams.timeTo = value[1];
      }
    }
  },
  data() {
    return {
      loading: false,
      users: [],
      tableData: [],
      total: 1,
      queryParams: {
        createBy: null,
        openid: null,
        timeFrom: '',
        timeTo: '',
        activityId: '',
        pageNum: 1,
        pageSize: 20
      },
      status: [
        { label: '正常', value: '1' },
        { label: '作废', value: '0' }
      ]
    };
  },
  created() {
    this.init();
  },
  methods: {
    init(data) {
      if (data == null) data = this.data;
      this.queryParams.activityId = data.activityId;
      this.getList();
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /**
     * 查询数据
     */
    getList() {
      this.loading = true;
      if (this.queryParams.activityId == '') return;

      targetedList(this.queryParams).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.tableData = res.rows;
        } else {
          ElMessage.error(res.msg);
        }
      });
    },

    /**
     * 获取用户列表
     */
    queryUserList(name) {
      listUser({ nickName: name }).then((res) => {
        if (res.code == 200) {
          this.users = res.rows;
        }
      });
    },

    /**
     * 匹配结果
     */
    marchType(value, types) {
      for (let i = 0; i < types.length; i++) {
        let type = types[i];
        if (type.value == value) return type.label;
      }
    }
  }
};
</script>
